package com.nutiteq.directions;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import com.nutiteq.components.WgsPoint;
import com.nutiteq.services.CloudMadeDirections;

public class CloudMadeOptionsForm extends Form implements CommandListener {
  private final DirectionsCanvas directionsCanvas;
  private final WgsPoint startCoordinates;
  private final WgsPoint endCoordinates;

  private final Command ok;
  private final Command back;
  private final ChoiceGroup vehicle;

  public CloudMadeOptionsForm(final DirectionsCanvas directionsCanvas,
      final WgsPoint startCoordinates, final WgsPoint endCoordinates) {
    super("CloudMade");
    this.directionsCanvas = directionsCanvas;
    this.startCoordinates = startCoordinates;
    this.endCoordinates = endCoordinates;

    vehicle = new ChoiceGroup("Transport", ChoiceGroup.POPUP);
    vehicle.append("Car", null);
    vehicle.append("Bicycle", null);
    vehicle.append("Foot", null);

    append(vehicle);
    ok = new Command("Ok", Command.OK, 0);
    back = new Command("Back", Command.BACK, 0);

    addCommand(ok);
    addCommand(back);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      directionsCanvas.show(directionsCanvas);
    } else if (cmd == ok) {
      final String v = getVehicle(vehicle.getSelectedIndex());
      directionsCanvas.setDirectionsService(new CloudMadeDirections(directionsCanvas,
          startCoordinates, endCoordinates, v, "B37E70903EB540C1A07B3437AFA5DF2E","TestUser"));
    }
  }

  private String getVehicle(final int selectedIndex) {
    if (selectedIndex == 0) {
      return CloudMadeDirections.ROUTE_TYPE_CAR;
    } else if (selectedIndex == 1) {
      return CloudMadeDirections.ROUTE_TYPE_BICYCLE;
    } else {
      return CloudMadeDirections.ROUTE_TYPE_FOOT;
    }
  }
}
